package com.example.smartcommunity;

import android.graphics.drawable.Drawable;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.RadioGroup;


import com.example.smartcommunity.com.example.adapter.MyFragmentPagerAdapter;
import com.example.smartcommunity.com.example.fragment.FindFragment;
import com.example.smartcommunity.com.example.fragment.HomeFragment;
import com.example.smartcommunity.com.example.fragment.MyFragment;
import com.example.smartcommunity.com.example.fragment.PhoneFragment;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {

    private ViewPager view_page;
    private RadioGroup mRadioGroup;
    private List<Fragment> fragmentList=new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        view_page =(ViewPager)findViewById(R.id.view_page);
        mRadioGroup =(RadioGroup)findViewById(R.id.mRadioGroup);

        HomeFragment homeFragment=new HomeFragment();
        FindFragment findFragment=new FindFragment();
        PhoneFragment phoneFragment=new PhoneFragment();
        MyFragment myFragment=new MyFragment();

        fragmentList.add(homeFragment);
        fragmentList.add(findFragment);
        fragmentList.add(phoneFragment);
        fragmentList.add(myFragment);

        view_page.setAdapter(new MyFragmentPagerAdapter(getSupportFragmentManager(),fragmentList));

        // ViewPager页面切换监听
        view_page.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                switch (position){
                    case 0:
                        mRadioGroup.check(R.id.home);
                        break;
                    case 1:
                        mRadioGroup.check(R.id.home_find);
                        break;
                    case 2:
                        mRadioGroup.check(R.id.home_phone);
                        break;
                    case 3:
                        mRadioGroup.check(R.id.home_my);
                        break;
                    default:
                        break;
                }
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });

        // RadioGroup选中状态改变监听
        mRadioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                switch (checkedId){
                    case R.id.home:
                        /**
                         * setCurrentItem第二个参数控制页面切换动画
                         * true:打开/false:关闭
                         */
                        view_page.setCurrentItem(0,true);

                        break;
                    case R.id.home_find:
                        view_page.setCurrentItem(1,true);
                        break;
                    case R.id.home_phone:
                        view_page.setCurrentItem(2);
                        break;
                    case R.id.home_my:
                        view_page.setCurrentItem(3);
                        break;
                    default:
                        break;
                }
            }
        });
    }
}